const db = require("../db/index");
const fs = require("fs");

const fileControll = {
  // 上传文件
  uploadFile: async (req, res) => {
    let oldName = req.files[0].filename;
    let newName = Buffer.from(req.files[0].originalname, "latin1").toString(
      "utf-8"
    );
    let upload_time = new Date();
    const sql1 = "SELECT * FROM files WHERE file_name = ?";
    const [data] = await db.query(sql1, newName);
    if (data.length) {
      return res.cc("文件已存在");
    }
    fs.renameSync("./public/upload/" + oldName, "./public/upload/" + newName);
    const sql = "INSERT INTO files SET ?";
    try {
      await db.query(sql, {
        file_url: `http://127.0.0.1:3000/upload/${newName}`,
        file_name: newName,
        file_size: (req.files[0].size * 1) / 1024,
        upload_time,
        download_number: 0,
      });
    } catch (err) {
      return res.cc(err);
    }
    res.send({
      status: 1,
      url: `http://127.0.0.1:3000/upload/${newName}`,
      data: newName,
    });
  },
  // 绑定上传者
  bingFileAndUser: async (req, res) => {
    const { name, url } = req.body;
    const sql = "UPDATE files SET upload_person = ? WHERE file_url = ?";
    try {
      await db.query(sql, [name, url]);
      return res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 更新下载量
  updateDownloadNumber: async (req, res) => {
    const { download_number, id } = req.body;
    console.log(download_number);

    const number = download_number * 1 + 1;
    const sql = `UPDATE files SET download_number = ? WHERE id = ?`;
    try {
      await db.query(sql, [number, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 下载文件
  downloadFile: async (req, res) => {
    const { id } = req.body;
    const sql = `SELECT * FROM files WHERE id = ?`;
    try {
      const [data] = await db.query(sql, id);
      res.send({
        status: 1,
        message: "成功",
        data: data[0].file_url,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取文件列表
  getFileList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM files ORDER BY upload_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = `SELECT * FROM files`;
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        data: data,
        total: total.length,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 搜索文件
  searchFile: async (req, res) => {
    const { file_name } = req.query;
    const sql = `SELECT * FROM files WHERE file_name LIKE ?`;
    try {
      const [data] = await db.query(sql, file_name);
      res.send({
        status: 1,
        data: data,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 删除文件
  deleteFile: async (req, res) => {
    const { id, file_name } = req.body;
    const sql = `DELETE FROM files WHERE id = ?`;
    try {
      await db.query(sql, id);
      fs.unlink(`./public/upload/${file_name}`, (err) => {
        if (err) return res.cc(err);
      });
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = fileControll;
